<template>
  <el-form :model="registerForm" :rules="rules" ref="registerForm" class="login-container" label-position="left"
           label-width="0px">
    <h3 class="login_title">账号注册</h3>
    <el-form-item prop="userName">
      <el-input type="text" v-model="registerForm.userName" auto-complete="off" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="registerForm.password" auto-complete="off" placeholder="密码" show-password>
      </el-input>
    </el-form-item>
    <el-form-item prop="phoneNumber">
      <el-input type="tel" v-model="registerForm.phoneNumber" auto-complete="off" placeholder="手机号">
      </el-input>
    </el-form-item>
    <el-form-item prop="verCode" class="getVerCode">
      <el-input type="text" maxlength="6" v-model="registerForm.verCode" auto-complete="off" placeholder="验证码">
        <template slot="append">
          <el-button plain style="border: 1px solid #909399" :disabled="time>0" @click="getVerCode()">{{getVerCodeStatus}}</el-button>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%;background: #505458;border: none" @click="register()">注册
      </el-button>
    </el-form-item>
    <el-form-item>已有账号？<el-button type="text" style="color:#1a5b59;font-weight:bold" @click="login()">登录</el-button></el-form-item>
  </el-form>
</template>

<script>
import api from '@/apis/api';
export default {
  name:'register',
  data() {
    return {
      time: 0,
      rules: {
        userName: [{
          required: true,
          message: '用户名不能为空',
          trigger: 'blur'
        },{
          pattern: /^[A-Za-z0-9]+$/,    //正则校验不用字符串
          message: "用户名不合法",
          trigger: "blur"
        },{
          min: 8, max: 15,
          message: '长度在 8 到 15 个字符',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '密码不能为空',
          trigger: 'blur'
        },{
          pattern: /^[A-Za-z0-9]+$/,    //正则校验不用字符串
          message: "密码不能包含特殊字符",
          trigger: "blur"
        },{
          min: 6, max: 18,
          message: '长度在 6 到 18 个字符',
          trigger: 'blur'
        }],
        phoneNumber: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        },{
          pattern: /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,    //正则校验不用字符串
          message: "请填写正确的手机号码",
          trigger: "blur"
        }],
        verCode: [{
          required: true,
          message: '请输入验证码',
          trigger: 'blur'
        },{
          pattern: /^\d{6}$/,
          message: "验证码为6位数字",
          trigger: "blur"
        }]
      },
      checked: true,
      interval: '', //计时器
      registerForm: {
        userName: '',
        password: '',
        phoneNumber: '',
        verCode: '',
      },
      loading: false
    }
  },
  computed:{
    getVerCodeStatus(){
      if (this.time === 0) {
        clearInterval(this.interval)
        return "发送验证码"
      }
      return `发送成功(${this.time})`
    }
  },
  mounted() {
    window.addEventListener('keydown', this.keyDown);
  },
  methods: {
    getVerCode(){
      if (/^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(this.registerForm.phoneNumber)){
        api.getVerCode(this.registerForm.phoneNumber).then(response=>{
          if (response.code === 200){
            let that = this
            that.$util.successMessage(response.message);
            that.time = 120;
            that.interval = setInterval(()=>{that.time--},1000)
          }
          else {this.$util.errorMessage(response.message)}
        })
      }else (
        this.$util.errorMessage("请正确填写手机号")
      )
    },
    register(){
      this.$refs.registerForm.validate(valid=>{
        if (valid){
          let data = {
            userName:this.registerForm.userName,
            password:this.registerForm.password,
            phoneNumber:this.registerForm.phoneNumber,
            verCode:this.registerForm.verCode,
          }
          api.register(data).then(response=>{
            if (response.code === 200) {
              this.$util.successMessage(response.message);
              this.login();
            }
          })
        }
      })
    },
    login(){
      this.$emit("status",'login')
    },
    keyDown(e) {
      if (e.keyCode === 13) {
        this.register()
      }
    },
  },
}
</script>

<style scoped>

</style>
